package zad2;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;

public class Zad2Test {

	public static void main(String[] args) {

		int liczbaProducentow = 1, liczbaKonsumentow = 1, rozmiarBuffora = 5;
		final Semafor semafor = new Semafor(rozmiarBuffora);
		final Semafor in = new Semafor(1);

		final Deque<String> deque = new ArrayDeque<String>();

		ArrayList<Thread> threads = new ArrayList<Thread>();

		// Tworzenie producentow
		for (int i = 0; i < liczbaProducentow; i++) {
			threads.add(new Thread() {
				@Override
				public void run() {
					while (true) {
						semafor.p();
						in.v();
						String val = Thread.currentThread().getName() + " "
								+ (System.nanoTime()/1000)/1000000.;
						deque.push(val);
						System.out.println(deque);
						in.p();
					}

				}
			});
		}

		// Tworzenie konsumentow
		for (int i = 0; i < liczbaKonsumentow; i++) {
			threads.add(new Thread() {
				@Override
				public void run() {
					while (true) {
						semafor.v();
						in.v();
						deque.pollFirst();
						System.out.println(deque);
						in.p();
					}

				}
			});
		}

		in.p();

		for (Thread thread : threads) {
			thread.start();
		}

	}

}
